package com.sanjiahantang.youjiaclient.module.common.models.impl;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.common.beans.ShopBean;
import com.sanjiahantang.youjiaclient.module.common.models.IShopModel;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchListBean;
import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;

/**
 * Created by zy on 2017/9/5.
 */

public class ShopModel implements IShopModel {
    @Override
    public void getShopData(final Context context, final CallBack<ShopBean> callBack) {
        Map<String,String> param = new HashMap<>();
        param.put("roleId","");
        if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)){
            param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        }
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/mall/phone/goodsPage/indexGoodsPageInfo",param,
                new BaseDelegate.ResultCallback<String>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(String response, Object tag) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject categoryFullList = jsonObject.getJSONObject("categoryFullList");
                    ShopBean bean = new ShopBean();
                    Iterator it = categoryFullList.keys();
                    List<ShopBean.Category> categoryList = new ArrayList<ShopBean.Category>();
                    Gson gson = new Gson();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        JSONArray array = categoryFullList.getJSONArray(key);
                        List<ShopBean.CategoryItem> items = new ArrayList<ShopBean.CategoryItem>();
                        for(int i=0;i<array.length();i++){
                            ShopBean.CategoryItem categoryItem = gson.fromJson(array.getString(i), ShopBean.CategoryItem.class);
                            items.add(categoryItem);
                        }
                        ShopBean.Category category = new ShopBean.Category();
                        category.setName(key);
                        category.setItemList(items);
                        categoryList.add(category);
                    }
                    bean.setCategoryFullList(categoryList);
                    List<ShopBean.Category> categoryList1 = gson.fromJson(jsonObject.getString("categoryList"),new TypeToken<List<ShopBean.Category>>(){}.getType());
                    bean.setCategoryList(categoryList1);

                    List<ShopBean.Advisement> adList = gson.fromJson(jsonObject.getString("goodsPageAdList"),new TypeToken<List<ShopBean.Advisement>>(){}.getType());
                    bean.setGoodsPageAdList(adList);

                    List<MatchListBean.Detail> details = gson.fromJson(jsonObject.getString("togeterList"),new TypeToken<List<MatchListBean.Detail>>(){}.getType());

                    bean.setTogeterList(details);
                    callBack.onSuccess(bean,context, tag);
                } catch (JSONException e) {
                    e.printStackTrace();
                    callBack.onFailure(e.getMessage());
                }
            }
        });
    }



}
